#include <iostream>
#include <vector>
#include <deque>
#include <list>

using std::cout;
using std::endl;
using std::vector;
using std::deque;
using std::list;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    void print() const
    {
        cout << "(" << _ix 
             << ", " << _iy
             << ")";
    }

    ~Point()
    {
        cout << "~Point()" << endl;
    }

private:
    int _ix;
    int _iy;
};

void test00()
{
    vector<Point> number;
    Point pt(1, 2);
    number.push_back(Point(1, 2));
    number.push_back({1, 2});
    number.push_back(pt);
    number.emplace_back(3, 4);
}

void test()
{
    cout << "sizeof(vector<int>) = " << sizeof(vector<int>) << endl;
    cout << "sizeof(vector<char>) = " << sizeof(vector<char>) << endl;

    cout << endl;
    vector<int> number = {1, 3, 5, 9, 7, 4, 6, 2, 10, 3};
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(33);
    number.push_back(77);
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    number.pop_back();
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;

    //Q：为什么vector不支持在头部进行插入与删除？
    //A:因为vector中的元素是连续的，如果在头部删除一个元素，
    //那么后面的元素都会前移；如果在头部进行插入一个元素，那么
    //后面的所有的元素都会后移，这样会导致一个元素的插入与删除
    //使得整个vector中的元素都要变动位置，时间复杂度O(N)
    //
    //Q:如何获取vector第一个元素的地址？
    &number;//error,获取不到第一个元素的地址
    &number[0];//ok,可以获取第一个元素的地址
    &*number.begin();//ok,可以获取第一个元素的地址
    int *pdate = number.data();//ok,可以获取第一个元素的地址

    cout << endl << "在vector的任意位置进行插入元素" << endl;
    vector<int>::iterator it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 100);
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    //迭代器失效（重要）
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 30, 200);
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    vector<int> vec = {77, 99, 33, 55};
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, {111, 444, 666, 888, 333});
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;

    cout << endl << "清空vector容器中的元素" << endl;
    number.clear();//只是将元素清空了，但是没有回收内存
    number.shrink_to_fit();//将剩余的空间进行了回收
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
}

void test2()
{
    deque<int> number = {1, 3, 5, 9, 7, 4, 6, 2, 10, 3};
    display(number);

    cout << endl << "在deque的尾部进行插入与删除" << endl;
    number.push_back(33);
    number.push_back(77);
    display(number);
    number.pop_back();
    display(number);

    cout << endl << "在deque的头部进行插入与删除" << endl;
    number.push_front(100);
    number.push_front(200);
    display(number);
    number.pop_front();
    display(number);

    cout << endl << "在deque的任意位置进行插入元素" << endl;
    deque<int>::iterator it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 100);
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    number.insert(it, 30, 200);
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    vector<int> vec = {77, 99, 33, 55};
    it = number.begin();
    ++it;
    ++it;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    number.insert(it, {111, 444, 666, 888, 333});
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << "清空deque容器中的元素" << endl;
    number.clear();//只是将元素清空了，但是没有回收内存
    number.shrink_to_fit();//将剩余的空间进行了回收
    cout << "number.size() = " << number.size() << endl;
}

void test3()
{
    list<int> number = {1, 3, 5, 9, 7, 4, 6, 2, 10, 3};
    display(number);

    cout << endl << "在list的尾部进行插入与删除" << endl;
    number.push_back(33);
    number.push_back(77);
    display(number);
    number.pop_back();
    display(number);

    cout << endl << "在list的头部进行插入与删除" << endl;
    number.push_front(100);
    number.push_front(200);
    display(number);
    number.pop_front();
    display(number);

    cout << endl << "在list的任意位置进行插入元素" << endl;
    list<int>::iterator it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 100);
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    number.insert(it, 30, 200);
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    vector<int> vec = {77, 99, 33, 55};
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    number.insert(it, {111, 444, 666, 888, 333});
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << "清空list容器中的元素" << endl;
    number.clear();//只是将元素清空了，但是没有回收内存
    cout << "number.size() = " << number.size() << endl;
}

int main(int argc, char **argv)
{
    test3();
    return 0;
}

